package edu.caltech.android.eew;

import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import edu.caltech.android.R;

public class DiscActivity extends MapActivity {
	private static final String TAG = "DiscActivity";
	private DiscOverlay discOverlay;

	private Handler handler = new Handler();

	private Runnable runnable = new Runnable() {
		int nRuns = 0;

		public void run() {
			nRuns++;
			if (nRuns < 80) {
				// Radius grows about 6km per second
				updateDisc(nRuns * 750);
			} else {
				updateDisc(0);
				nRuns = 0;
			}
			/* and here comes the "trick" */
			handler.postDelayed(this, 125);
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_disc);
		MapView mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		List<Overlay> mapOverlays = mapView.getOverlays();
		// Drawable drawable =
		// this.getResources().getDrawable(R.drawable.androidmarker);
		// drawable.setAlpha(128);

		// GeoPoint mexicoCity = new GeoPoint(19240000,-99120000);

		// LocationsOverlay locationsOverlay = new LocationsOverlay(drawable,
		// this);
		// OverlayItem overlayitem = new OverlayItem(mexicoCity, "Hola, Mundo!",
		// "I'm in Mexico City!");
		// locationsOverlay.addOverlay(overlayitem);
		// mapOverlays.add(locationsOverlay);

		// int discRadius = 10000; // meters

		GeoPoint initialPosition;
		GeoPoint eventPosition;
		double magnitude = 0.0;
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			double user_lat = extras.getDouble("user_lat");
			double user_lon = extras.getDouble("user_lon");
			double event_lat = extras.getDouble("event_lat");
			double event_lon = extras.getDouble("event_lon");
			magnitude = extras.getDouble("event_mag");
			initialPosition = createGeoPoint(user_lat, user_lon);
			eventPosition = createGeoPoint(event_lat, event_lon);
			Log.i(TAG, "got quake event parameters");
			setTitle("Magnitude: " + Double.toString(magnitude));
		} else {
			// Caltech
			initialPosition = createGeoPoint(34.144168, -118.11852);
			// Somewhere north of Caltech:
			eventPosition = createGeoPoint(34.144, -118.119);
		}

		discOverlay = new DiscOverlay(eventPosition, 0, this);
		mapOverlays.add(discOverlay);

		UserPositionOverlay userOverlay = new UserPositionOverlay(
				initialPosition, this);
		mapOverlays.add(userOverlay);

		MapController mc = mapView.getController();
		mc.animateTo(initialPosition);
		mc.setZoom(11);
		mapView.invalidate();

		// Animate
		handler.postDelayed(runnable, 100);

	}

	// @Override
	// public boolean onOptionsItemSelected(MenuItem item) {
	// switch (item.getItemId()) {
	// case android.R.id.home:
	// NavUtils.navigateUpFromSameTask(this);
	// return true;
	// }
	// return super.onOptionsItemSelected(item);
	// }

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	private void updateDisc(int radiusMeters) {
		// TODO: this shouldn't run if this activity is not visible.
		discOverlay.setRadius(radiusMeters);
		MapView mapView = (MapView) findViewById(R.id.mapview);
		mapView.postInvalidate();

	}

	/**
	 * Convenience function for converting to microdegrees.
	 * 
	 * @param lat
	 *            Degrees latitude
	 * @param lng
	 *            Degrees longitude
	 * @return
	 */
	private GeoPoint createGeoPoint(double lat, double lng) {
		GeoPoint geoPoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
		return geoPoint;
	}

}
